#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = "zhangchitc@gmail.com"


import cookielib
import urllib2, urllib
import xndata.client
from xndata.frontend.note import NotePageFetcher
from xndata.frontend.note import NotePageParser

class XNNoteClient (xndata.client.XNClient):

    def __init__ (self, xnclient=None, uname=None, upass=None):
        if xnclient is not None:
            # initialize with already-logined xnclient
            self.opener = xnclient.opener
            self.user_id = xnclient.user_id
            self.user_name = xnclient.user_name

        if (uname is not None) and (upass is not None):
            # initialize with username and password
            xndata.client.XNClient.__init__ (self, uname, upass)


    def getTotalNote (self, user_id):
        if user_id == self.user_id:
            html = NotePageFetcher.fetch_my_note_list_page (self.opener, 0)
        else:
            html = NotePageFetcher.fetch_others_note_list_page (
                        self.opener, user_id, 0)
        return NotePageParser.get_total_note (html)

    def getTotalNoteList (self, user_id):
        return (self.getTotalNote (user_id) - 1) / 10 + 1

    def getNextNoteList (self, user_id=None, prev_note_list=None):
        if user_id is None:
            user_id = prev_note_list.author_id
            cur_page = prev_note_list.cur_page + 1
        else:
            cur_page = 0

        if user_id == self.user_id:
            html = NotePageFetcher.fetch_my_note_list_page (self.opener, cur_page)
        else:
            html = NotePageFetcher.fetch_others_note_list_page (
                        self.opener, user_id, cur_page)

        next_note_list = NotePageParser.get_note_list (html, user_id, cur_page)
        return next_note_list

    def getAllNote (self, user_id):
        total_note_list = self.getTotalNoteList (user_id)
        all_note_list = self.getNextNoteList (user_id=user_id)
        prev_res = all_note_list
        for i in range (1, total_note_list):
            tmp_note_list = self.getNextNoteList (prev_note_list=prev_res)
            all_note_list.nlist.extend (tmp_note_list.nlist)
            prev_res = tmp_note_list

        return all_note_list

    def getNote (self, user_id, note_id):
        html = NotePageFetcher.fetch_note_page (self.opener, user_id, note_id)
        return NotePageParser.get_note (html, user_id, note_id)


if __name__ == "__main__":
    client = XNStatusClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")
    
    print client.getTotalStatus ("247780420")
    
    # Other's albums
    status_list = client.getNextStatusList (user_id="247780420")
    #print status_list
    status_list = client.getNextStatusList (prev_status_list=status_list)
    print status_list
   # friend_list2 = client.getNextFriendList (prev_friend_list=friend_list1)
    #print friend_list2


    status_list = client.getAllStatus (user_id="242124580")
    print status_list

    # other's album
    #photo_list = client.getNextPhotoList (user_id="247780420", album_id="282652960")
    #print photo_list
    # my album
    #photo_list = client.getNextPhotoList (user_id="242124580", album_id="432244996")
    #print photo_list
    #print client.getAllPhoto ("242124580", "432244996")
    #print client.getAllPhoto ("247780420", "282652960")
    # print client.getAllPhoto ("252475509", "265669703")
    #print client.getAllPhoto ("242124580", "272296924")
